package app.controllers;

import app.models.TemporalModel;
import framework.FRDModel;
import framework.temporal.FRDTemporal;
import framework.temporal.FRDTemporalController;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Lukáš Marek
 */
public class TemporalController extends FRDTemporalController
{
	private JTextField intervalOd;
  private JTextField intervalDo;
	private JButton deleteIntervalButton;
	
	public TemporalController(FRDTemporal fRDTemporal1, FRDModel model)
	{
		super(fRDTemporal1, model);
		AddComponents();
	}

	/**
	 * Vlozeni novych grafickych komponent
	 */
	private void AddComponents()
	{
		JLabel jLabel5 = new JLabel("Interval");
    jLabel5.setBounds(140, 10, 50, 20);
    temporalPanel.jLayeredPane2.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
		JLabel jLabel7 = new JLabel("od:");
    jLabel7.setBounds(200, 10, 20, 20);
    temporalPanel.jLayeredPane2.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);
		JLabel jLabel6 = new JLabel("do:");
    jLabel6.setBounds(300, 10, 20, 20);
    temporalPanel.jLayeredPane2.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);
		intervalOd = new JTextField();
    intervalOd.setBounds(220, 10, 70, 20);
    temporalPanel.jLayeredPane2.add(intervalOd, javax.swing.JLayeredPane.DEFAULT_LAYER);
		intervalDo = new JTextField();
    intervalDo.setBounds(320, 10, 70, 20);
    temporalPanel.jLayeredPane2.add(intervalDo, javax.swing.JLayeredPane.DEFAULT_LAYER);
    deleteIntervalButton = new JButton("Odstranit interval");
    deleteIntervalButton.setBounds(400, 10, 150, 20);
		deleteIntervalButton.setActionCommand("deleteIntervalButton");
    temporalPanel.jLayeredPane2.add(deleteIntervalButton, javax.swing.JLayeredPane.DEFAULT_LAYER);
		deleteIntervalButton.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		switch (button.getActionCommand())
		{
			case "deleteIntervalButton":
				int from,to;
				try
				{
					from = Integer.parseInt(intervalOd.getText());
					to = Integer.parseInt(intervalDo.getText());
				}
				catch (NumberFormatException ex)
				{
					System.err.println("Integer.parseInt: " + ex.toString());
					break;
				}
				for (int i = 0; i < temporalPanel.jTable.getSelectedRows().length; i++)
				{
					String row = (String) temporalPanel.jTable.getModel().getValueAt(temporalPanel.jTable.getSelectedRows()[i], 0);
					((TemporalModel)model).deleteInterval(Integer.parseInt(row), from, to);
				}
				loadItems();
				temporalPanel.repaint();
				break;
			default:
				super.actionPerformed(e);
				break;
		}
	}
}
